"""
Copyright (c) 2022 Guangdong University of Technology
PhotLab is licensed under [Open Source License].
You can use this software according to the terms and conditions of the [Open Source License].
You may obtain a copy of [Open Source License] at: [https://open.source.license/]

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.

See the [Open Source License] for more details.

Author: Meng Xiang, Sailan Yan
Created: 2023/8/19
Supported by: National Key Research and Development Program of China
"""


import numpy as np

import phot
from phot.components.tx.PBC import PBC


def PBS(input):
    """偏振分束器

    Args:
        input：输入信号

    Returns:
        output_1：分束后的信号1
        output_2：分束后的信号2

    """

    input_I = np.expand_dims(np.real(input), axis=1)
    input_Q = np.expand_dims(np.imag(input), axis=1)
    epsilon_x = 1
    epsilon_y = 0  # 1
    sigma_x = 1    # 0
    sigma_y = 0
    output_X_I = epsilon_x * input_I
    output_X_Q = sigma_x * input_Q
    output_Y_I = sigma_y * input_I
    output_Y_Q = epsilon_y * input_Q
    output_1 = output_X_I + 1j * output_X_Q
    output_2 = output_Y_I + 1j * output_Y_Q
    return output_1, output_2


if __name__ == "__main__":
    phot.config(plot=False, backend="numpy")  # 全局开启画图，backend 使用 numpy

    # 设置全局系统仿真参数
    num_symbols = 2**16  # 符号数目
    bits_per_symbol = 4  # 2 for QPSK; 4 for 16QAM; 6 for 64QAM  设置调制格式
    total_baud = 30e9  # 信号波特率，符号率
    up_sampling_factor = 2  # 上采样倍数
    sampling_rate = up_sampling_factor * total_baud  # 信号采样率
    Reference_frequency = 193.1e12

    # 首先产生发射端X/Y双偏振信号
    signal_bits = phot.gen_bits(num_symbols * bits_per_symbol)

    # 生成双偏振符号序列 #
    signals = phot.Modulation(signal_bits, bits_per_symbol)

    data = signals[0]

    output_1, output_2 = PBS(data)

    output = PBC(output_1, output_2)

    # out = output_1 * output_2
    print(np.real(output))









